import axios from './config'
import vue from '../main'
var newsUtil = {
  list: function () {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      axios.post('/mallApi/mall/common/news/list', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  editPre: function (nid) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('nid', nid)
      axios.post('/mallApi/mall/common/news/editPre', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  loadNews: function (nid) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('nid', nid)
      axios.post('/mallApi/mall/common/news/editPre', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  loadComment: function (currentPage) {
    console.log('****** 当前页：currentPage = ' + currentPage)
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('cp', currentPage)
      axios.post('/mallApi/mall/common/news/comment/list', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
}
var userUtil = {
  loadRole: function () {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      axios.post('/mallApi/mall/back/role/list', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  loadAction: function (rid) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('rid', rid)
      axios.post('/mallApi/mall/back/action/list', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  loadUser: function (currentPage) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('cp', currentPage)
      axios.post('/mallApi/mall/back/user/list', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
}
var goodsUtil = {
  loadItem: function () {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      axios.post('/mallApi/mall/common/item/item', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  loadSubitem: function (iid) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('pid', iid)
      axios.post('/mallApi/mall/common/item/subitem', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  goodsAdd: function (goods) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      for (var key in goods) {
        console.log('------'+key+'----'+goods[key])
        params.append(key, goods[key])
      }
      console.log('==params=='+params)
      axios.post('/api/goods/add', params) // /api/problem.action
        .then(response => {
          if (response.success) {
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          } else {
            this.$message.error(response.msg)
          }
          resolve(response.data) // 返回处理结果
          console.log('===' + response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  goodsEdit: function (goods) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      for (var key in goods) {
        params.append(key, goods[key])
      }
      axios.post('/mallApi/mall/goods/edit', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  goodsLoad: function (gid) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('gid', gid) // 设置要查询的商品信息
      axios.post('/mallApi/mall/goods/get', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  loadComment: function (currentPage) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('cp', currentPage) // 设置要查询的商品信息
      axios.post('/mallApi/mall/goods/comment/list', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  removeComment: function (cid) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('cid', cid) // 设置要查询的商品信息
      axios.post('/mallApi/mall/goods/comment/remove', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  goodsList: function (currentPage) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('cp', currentPage) // 设置要查询的商品信息
      axios.post('/mallApi/mall/goods/list', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
}
var killUtil = {
  add: function (data) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      for (var key in data) {
        params.append(key, data[key])
      }
      axios.post('/mallApi/mall/goods/kill/add', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  loadKill: function () {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      axios.post('/mallApi/mall/goods/kill/list', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  remove: function (kid) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('kid', kid)
      axios.post('/mallApi/mall/goods/kill/remove', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
}
var ordersUtil = {
  loadData: function (currentPage) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('cp', currentPage)
      axios.post('/mallApi/mall/orders/list', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  loadDetails: function (oid) {
    return new Promise((resolve, reject) => {
      var params = new URLSearchParams() // 必须使用此方式进行数据传递
      params.append('oid', oid)
      axios.post('/mallApi/mall/orders/details', params) // /api/problem.action
        .then(response => {
          resolve(response.data) // 返回处理结果
        })
        .catch((error) => {
          reject(error)
        })
    })
  }
}
export default { // 所有可以导出使用的方法都必须在此处定义
  news: newsUtil,
  user: userUtil,
  goods: goodsUtil,
  kill: killUtil,
  orders: ordersUtil
}
